package org.leuyius.app.colife.colife.uas.intrerceptor;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PerformanceMonitoringInterceptor {
/*
	*//**
	 * logger 日志信息
	 *//*
	private static final Logger logger = LoggerFactory
			.getLogger(PerformanceMonitoringInterceptor.class);
	
	private static final String NULL_OPER_CHANNL_ID = "0" ;
	private static final String NULL_OPER_CHANNL_NAME = "匿名用户" ;
	private static final String SYSTEM_INTERFACE_STATUS_SUCCESS = "success" ;
	private static final String SYSTEM_INTERFACE_STATUS_DEFAULT = "fail" ;
	private static final String CONTROLLER_RESULT_STATUS = "state" ;
	
	private static final String LOG_PRE = "common" ;
	private static final String LOG_LOGIN_PRE = "login" ;
	private static final String LOG_LOGIN_CLASS = "cn.com.karakal.micro.website.admin.web.AdminController.login" ;
	
	*//**
	 * Description: 打印接口日志信息<BR>
	 *
	 * @param pjd
	 * @return
	 * @throws Throwable
	 * @author leuyius
	 * @date 2017年8月8日 下午2:45:30
	 * @version 1.0
	 *//*
	@Around("execution(* cn.com.karakal.micro.website.admin.web.*Controller.*(..))")
    public Object printOperLog(ProceedingJoinPoint pjd) throws Throwable{
		//@ TODO 如果出现404请求转发时，此地址将会显示的是404地址，而不是之前请求的地址
		// 1 接口执行前的相关初始化操作（为了更小的影响统计接口处理时间，日志参数初始化信息在接口调用完成后初始化）
		String operMethod = pjd.getSignature().getDeclaringTypeName() + "." + pjd.getSignature().getName() ;
		long startTime = System.currentTimeMillis();
		String operStatus = SYSTEM_INTERFACE_STATUS_DEFAULT ;
		Object result = null ;
		Throwable exception = null ;
		String exceptionMsg = "" ;
		String resultMsg = "" ;
		Map<String, String[]> paramterMap = null ; 
		try {
			result = pjd.proceed();//执行操作
			if(null != result && result instanceof String){
				resultMsg = (String) result ;
			}
			if(null != result && !(result instanceof String)){
				resultMsg = JSON.toJSONString(result) ;
				JSONObject jsonObject = JSONObject.parseObject(resultMsg) ;
				if (jsonObject.containsKey(CONTROLLER_RESULT_STATUS) &&
						ResponseState.SUCCESS.getState().equals(jsonObject.getString(CONTROLLER_RESULT_STATUS))) {
					operStatus = SYSTEM_INTERFACE_STATUS_SUCCESS ;
				}
			}
		} catch (Exception e) {
			exception = e ;
			exceptionMsg = e.getMessage() ;
			logger.error("日志打印时出错", e);
		} catch (Throwable e) {
			exception = e ;
			operStatus = SYSTEM_INTERFACE_STATUS_DEFAULT ;
			exceptionMsg = e.getMessage() ;
		}
		
		// 2 获取到相关参数
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		HttpSession session = request.getSession();
		paramterMap = request.getParameterMap() ;
		String operUri = request.getRequestURI();//请求地址
		String loginPrefix = LOG_PRE ;
		long endTime = System.currentTimeMillis();
		long castTime = endTime - startTime ;
		String operIp = CrtlUtil.getRealIp(request) ;
		AdminLoginDTO adminDO = null ;
		if(LOG_LOGIN_CLASS.equals(operMethod)){
			loginPrefix = LOG_LOGIN_PRE ;
		}
		adminDO = SessionUtils.getCurrentUser(session) ;
		if(adminDO == null){
			adminDO = new AdminLoginDTO() ;
//			adminDO.setId(NULL_OPER_CHANNL_ID);
			adminDO.setUsername(NULL_OPER_CHANNL_NAME);
		}
		
		// 3 处理操作日志信息
		LogDO logDO = new LogDO(loginPrefix, adminDO.getUsername(), NULL_OPER_CHANNL_ID, 
				startTime, castTime, operIp, operUri, operMethod, operStatus, 
				resultMsg, exceptionMsg, paramterMap) ;
//		logger.info(JSON.toJSONString(logDO));
		
		// 4  处理异常信息并打印日志信息
		if(null != exception){
			throw exception ;
		}
		return result;
    }*/
	
}
